package com.example.idachuappone.order.imageutil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore.Audio.Albums;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;

import com.example.idachuappone.order.entity.ImageBucket;
import com.example.idachuappone.order.entity.ImageItem;

public class AlbumHelper {
	final String TAG;
	List<HashMap<String, String>> albumList;
	HashMap<String, ImageBucket> bucketList;
	Context context;
	ContentResolver cr;
	boolean hasBuildImagesBucketList;
	HashMap<String, String> thumbnailList;

	public AlbumHelper() {
		this.TAG = getClass().getSimpleName();
		this.thumbnailList = new HashMap();
		this.albumList = new ArrayList();
		this.bucketList = new HashMap();
		this.hasBuildImagesBucketList = false;
	}

	public void init(Context context) {
		if (this.context == null) {
			this.context = context;
			this.cr = context.getContentResolver();
		}
	}

	private void getThumbnail() {
		getThumbnailColumnData(this.cr.query(Thumbnails.EXTERNAL_CONTENT_URI,
				new String[] { "_id", "image_id", "_data" }, null, null, null));
	}

	private void getThumbnailColumnData(Cursor cur) {
		if (cur.moveToFirst()) {
			int _idColumn = cur.getColumnIndex("_id");
			int image_idColumn = cur.getColumnIndex("image_id");
			int dataColumn = cur.getColumnIndex("_data");
			do {
				int i = cur.getInt(_idColumn);
				int image_id = cur.getInt(image_idColumn);
				this.thumbnailList.put(image_id + "", cur.getString(dataColumn));
			} while (cur.moveToNext());
		}
	}

	void getAlbum() {
		getAlbumColumnData(this.cr.query(Albums.EXTERNAL_CONTENT_URI, new String[] { "_id", "album", "album_art",
				"album_key", "artist", "numsongs" }, null, null, null));
	}

	private void getAlbumColumnData(Cursor cur) {
		if (cur.moveToFirst()) {
			int _idColumn = cur.getColumnIndex("_id");
			int albumColumn = cur.getColumnIndex("album");
			int albumArtColumn = cur.getColumnIndex("album_art");
			int albumKeyColumn = cur.getColumnIndex("album_key");
			int artistColumn = cur.getColumnIndex("artist");
			int numOfSongsColumn = cur.getColumnIndex("numsongs");
			do {
				int _id = cur.getInt(_idColumn);
				String album = cur.getString(albumColumn);
				String albumArt = cur.getString(albumArtColumn);
				String albumKey = cur.getString(albumKeyColumn);
				String artist = cur.getString(artistColumn);
				int numOfSongs = cur.getInt(numOfSongsColumn);
				Log.i(this.TAG,
						new StringBuilder(String.valueOf(_id)).append(" album:").append(album).append(" albumArt:")
								.append(albumArt).append("albumKey: ").append(albumKey).append(" artist: ")
								.append(artist).append(" numOfSongs: ").append(numOfSongs).append("---").toString());
				HashMap<String, String> hash = new HashMap();
				hash.put("_id", new StringBuilder(String.valueOf(_id)).toString());
				hash.put("album", album);
				hash.put("albumArt", albumArt);
				hash.put("albumKey", albumKey);
				hash.put("artist", artist);
				hash.put("numOfSongs", new StringBuilder(String.valueOf(numOfSongs)).toString());
				this.albumList.add(hash);
			} while (cur.moveToNext());
		}
	}

	void buildImagesBucketList() {
		ImageBucket bucket;
		long startTime = System.currentTimeMillis();
		getThumbnail();
		Cursor cur = this.cr.query(Media.EXTERNAL_CONTENT_URI, new String[] { "_id", "bucket_id", "picasa_id", "_data",
				"_display_name", "title", "_size", "bucket_display_name" }, null, null, null);
		if (cur.moveToFirst()) {
			int photoIDIndex = cur.getColumnIndexOrThrow("_id");
			int photoPathIndex = cur.getColumnIndexOrThrow("_data");
			int photoNameIndex = cur.getColumnIndexOrThrow("_display_name");
			int photoTitleIndex = cur.getColumnIndexOrThrow("title");
			int photoSizeIndex = cur.getColumnIndexOrThrow("_size");
			int bucketDisplayNameIndex = cur.getColumnIndexOrThrow("bucket_display_name");
			int bucketIdIndex = cur.getColumnIndexOrThrow("bucket_id");
			int picasaIdIndex = cur.getColumnIndexOrThrow("picasa_id");
			int count = cur.getCount();
			do {
				String _id = cur.getString(photoIDIndex);
				String name = cur.getString(photoNameIndex);
				String path = cur.getString(photoPathIndex);
				String title = cur.getString(photoTitleIndex);
				String size = cur.getString(photoSizeIndex);
				String bucketName = cur.getString(bucketDisplayNameIndex);
				String bucketId = cur.getString(bucketIdIndex);
				Log.i(this.TAG,
						new StringBuilder(String.valueOf(_id)).append(", bucketId: ").append(bucketId)
								.append(", picasaId: ").append(cur.getString(picasaIdIndex)).append(" name:")
								.append(name).append(" path:").append(path).append(" title: ").append(title)
								.append(" size: ").append(size).append(" bucket: ").append(bucketName).append("---")
								.toString());
				bucket = this.bucketList.get(bucketId);
				if (bucket == null) {
					bucket = new ImageBucket();
					this.bucketList.put(bucketId, bucket);
					bucket.imageList = new ArrayList();
					bucket.bucketName = bucketName;
				}
				bucket.count++;
				ImageItem imageItem = new ImageItem();
				imageItem.imageId = _id;
				imageItem.imagePath = path;
				imageItem.thumbnailPath = this.thumbnailList.get(_id);
				bucket.imageList.add(imageItem);
			} while (cur.moveToNext());
		}
		for (Entry<String, ImageBucket> entry : this.bucketList.entrySet()) {
			bucket = entry.getValue();
			Log.d(this.TAG, new StringBuilder(String.valueOf(entry.getKey())).append(", ").append(bucket.bucketName)
					.append(", ").append(bucket.count).append(" ---------- ").toString());
			for (int i = 0; i < bucket.imageList.size(); i++) {
				ImageItem image = bucket.imageList.get(i);
				Log.d(this.TAG, "----- " + image.imageId + ", " + image.imagePath + ", " + image.thumbnailPath);
			}
		}
		this.hasBuildImagesBucketList = true;
		Log.d(this.TAG, "use time: " + (System.currentTimeMillis() - startTime) + " ms");
	}

	public List<ImageBucket> getImagesBucketList(boolean refresh) {
		if (refresh || !(refresh || this.hasBuildImagesBucketList)) {
			buildImagesBucketList();
		}
		List<ImageBucket> tmpList = new ArrayList();
		for (Entry<String, ImageBucket> entry : this.bucketList.entrySet()) {
			tmpList.add(entry.getValue());
		}
		return tmpList;
	}

	String getOriginalImagePath(String image_id) {
		Log.i(this.TAG, "---(^o^)----" + image_id);
		Cursor cursor = this.cr.query(Media.EXTERNAL_CONTENT_URI, new String[] { "_id", "_data" }, "_id=" + image_id,
				null, null);
		if (cursor == null) {
			return null;
		}
		cursor.moveToFirst();
		return cursor.getString(cursor.getColumnIndex("_data"));
	}
}